//
//  LJInPutTool.m
//  ReceiptDelivery
//
//  Created by 吕晶 on 2020/7/8.
//  Copyright © 2020 吕晶. All rights reserved.
//

#import "LJInPutTool.h"

//输入框高度
#define View_H 227

@interface LJInPutTool () <UITextViewDelegate>

/***键盘高度***/
@property (nonatomic, assign)CGFloat keyboardHeight;

// 发送按钮
@property (nonatomic, strong) UIButton *sendBtn;
@property (nonatomic, strong) UIButton *cancleBtn;

@end

@implementation LJInPutTool

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.frame = CGRectMake(0, 0, ScreenW, ScreenH);
        [self setupSubviews];
        [self addEventListening];
    }
    return self;
}

// 添加通知
-(void)addEventListening
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHidden:) name:UIKeyboardWillHideNotification object:nil];
}

-(void)setPlaceholder:(NSString *)placeholder {
    _placeholder = placeholder;
    self.textInput.placeholder = placeholder;
}

-(void)setSureBtnText:(NSString *)sureBtnText {
    _sureBtnText = sureBtnText;
    [self.sendBtn setTitle:sureBtnText forState:0];
}

-(void)setCancleBtnText:(NSString *)cancleBtnText {
    _cancleBtnText = cancleBtnText;
    [self.cancleBtn setTitle:cancleBtnText forState:0];
}

-(void)setupSubviews {
    self.bgView = [[UIView alloc] initWithFrame:CGRectMake(0, ScreenH, ScreenW, View_H)];
    self.bgView.backgroundColor = HexColor(0xffffff);
    [self addSubview:self.bgView];
    self.textInput = [[IQTextView alloc] initWithFrame:CGRectMake(15, 15, ScreenW - 30, 148)];
    self.textInput.font = [UIFont systemFontOfSize:15];
    self.textInput.layer.cornerRadius = 6;
    self.textInput.backgroundColor = HexColor(0xF2F2F5);
    self.textInput.layer.masksToBounds = YES;
    self.textInput.returnKeyType = UIReturnKeySend;
    self.textInput.enablesReturnKeyAutomatically = YES;
    self.textInput.delegate = self;
    self.textInput.placeholder = @"请输入驳回原因";
    [self.bgView addSubview:self.textInput];
    
    self.cancleBtn = [[UIButton alloc] initWithFrame:CGRectMake(30, self.textInput.nim_bottom + 10, (ScreenW - 60 - 15) * 0.5, 44)];
    [self.cancleBtn setTitle:@"取消驳回" forState:0];
    [self.cancleBtn setTitleColor:HexColor(0x585974) forState:0];
    self.cancleBtn.titleLabel.font = BoldFontOfSize(16);
    self.cancleBtn.layer.cornerRadius = 6;
    self.cancleBtn.layer.borderWidth = 1;
    self.cancleBtn.layer.borderColor = HexColor(0xDBDCE6).CGColor;
    [self.cancleBtn addTarget:self action:@selector(dismissView) forControlEvents:UIControlEventTouchUpInside];
    [self.bgView addSubview:self.cancleBtn];
    
    self.sendBtn = [[UIButton alloc] initWithFrame:CGRectMake(self.cancleBtn.nim_right + 15, self.cancleBtn.nim_top, self.cancleBtn.nim_width, self.cancleBtn.nim_height)];
    [self.sendBtn setTitle:@"确认驳回" forState:0];
    [self.sendBtn setTitleColor:HexColor(0xffffff) forState:0];
    [self.sendBtn setBackgroundColor:HexColor(0xF35959)];
    self.sendBtn.titleLabel.font = BoldFontOfSize(16);
    self.sendBtn.layer.cornerRadius = 6;
    [self.sendBtn addTarget:self action:@selector(sendClick) forControlEvents:UIControlEventTouchUpInside];
    [self.bgView addSubview:self.sendBtn];
    
    self.backgroundColor = HexColorA(0x000000, 0.5);//整个输入框的外框颜色
}

- (void)sendClick {
    if (self.finishBlock) {
        self.finishBlock(self.textInput.text);
        [self dismissView];
    }
}

- (void)keyboardWillShow:(NSNotification *)notification {
    CGRect keyboardFrame = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
    _keyboardHeight = keyboardFrame.size.height;
    CGFloat duration = [notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationBeginsFromCurrentState:YES];
    [UIView setAnimationDuration:duration];
    [UIView setAnimationCurve:7];
    self.bgView.nim_top = ScreenH - keyboardFrame.size.height - self.bgView.nim_height;
    [UIView commitAnimations];
}
- (void)keyboardWillHidden:(NSNotification *)notification
{
    CGRect keyboardFrame = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
    _keyboardHeight = keyboardFrame.size.height;
    CGFloat duration = [notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    [UIView animateWithDuration:duration animations:^{
        [self dismissView];
    }];
}

- (void)showInView:(UIView *)view {
    if (!view) {
        return;
    }
    [self addSubview:self.bgView];
    [self.textInput becomeFirstResponder];
    [view addSubview:self];
}

- (void)dismissView {
    [self.textInput resignFirstResponder];
    self.bgView.nim_top = ScreenH;
    [self.bgView removeFromSuperview];
    [self removeFromSuperview];
}

//- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
//{
//    // 点击return按钮
//    if ([text isEqualToString:@"\n"]){
//        if ([_delegate respondsToSelector:@selector(inputToolbar:sendContent:)]) {
//            [_delegate inputToolbar:self sendContent:self.textInput.text];
//        }
//        return NO;
//    }
//    return YES;
//}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

@end
